package world;

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;
import java.util.StringTokenizer;

import javax.swing.JFrame;

import Default.Manager;
import Default.SensorsPanel;
import Default.Variables;
import Default.WorldPanel;


public class World {
	
	double maskVolArr[]=new double[Variables.numDifferentVol];
	public ArrayList<Target> targets=null;
	public static ArrayList<double[][]> masks=null;
	double senseTable[][];
	//int x,y;
	int speed=10;
	public JFrame frame=null;
	
	public static int numOFtargets=0;
	public BufferedWriter out=null;
	BufferedReader in =null;
	public World(/*int y, int x,int radius*/) {
		
		setDifferentVol();
		senseTable=new double[Variables.y][Variables.x];
		masks= new ArrayList<double[][]>();
		makeVolMask(Variables.sense_radius);
		//this.x=Variables.x;
		//this.y=Variables.y;
		targets= new ArrayList<Target>();
		
		
		
		
	}
	public void openFileRead()
	{
		String file=Variables.targetsFileName+".txt";
		try{
			in=new BufferedReader(new FileReader(file));
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public void openFileWrite()
	{
		String file=Variables.targetsFileName+".txt";
		try{
			out=new BufferedWriter(new FileWriter(file));
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public void closeFile()
	{
		try{
			if(out!=null)
				out.close();
			if(in!=null)
				in.close();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	public void setDifferentVol()
	{
		maskVolArr[0]=25;
	}
	public void makeVolMask(int radius)
	{
		double tmpMask[][];
		int i,j;
		double distance,maskVol,tmpVol;
		DecimalFormat df=new DecimalFormat("00.000");
		for(int k=0;k<Variables.numDifferentVol;k++)
		{
			//tmpMask=masks.get(k);
			maskVol=maskVolArr[k];
			tmpMask=new double[radius*2+1][radius*2+1];
			/**
			 * need to fill the current vol every spot on mask
			 */
			//System.out.println("hi "+maskVol);
			for (i=0;i<radius*2+1;i++)
			{
				
				for(j=0;j<radius*2+1;j++)
				{
					distance=(Math.sqrt(Math.pow(Math.abs(radius-i), 2)+Math.pow(Math.abs(radius-j), 2)))/*TADA!!!*/;
					if(distance!=0)
					{
						tmpVol=maskVol/(Math.pow(distance,2));
						if(tmpVol>=1)
							tmpMask[i][j]=tmpVol;
					//System.out.println(distance+" "+maskVol/(Math.pow(distance,2)));
				}
					else
					{
						tmpMask[i][j]=maskVol;
						//System.out.println("hi "+distance);
					}
					//System.out.print(df.format(tmpMask[i][j])+" ");
				}
				//System.out.print(df.format(tmpMask[i][j])+" ");
			}
			masks.add(tmpMask);
			/*for(i=0;i<radius*2+1;i++)
			{
				for(j=0;j<radius*2+1;j++)
					System.out.print(df.format(masks.get(k)[i][j])+" ");
				System.out.println();
			}*/
		}
	}
	public void getSense()
	{
		/**
		 for each target in array
		 1.calculate new spot at this time
		 2.add the right mask to the senseTable
		 * 
		 */
	}
	
	
	public void generateTargetsToFile()
	{
		Random rand=new Random();
		
		try{
		
		//if(rand.nextInt()%2==1)//every second time step in average generate target
		//{
		if(targets.isEmpty())
		{
		//System.out.println("hi");
			
			//first number is how many targets we nee to generate
			out.write("1");
			out.write(" ");
			Target target=new Target();
			
			target.volume=rand.nextInt()%Variables.numDifferentVol;
			//second number is the volume
			out.write(Integer.toString(target.volume));
			out.write(" ");
			target.start_time=Time.time;
			if(rand.nextInt()%2==1)//decide on up-down direction
			{
				target.start.y=0+Variables.sense_radius;
				target.end.y=Variables.y+Variables.sense_radius-1;
			}
			else
			{
				target.start.y=Variables.y+Variables.sense_radius-1;
				target.end.y=0+Variables.sense_radius;
			}
			out.write(Integer.toString(target.start.y));
			out.write(" ");
			out.write(Integer.toString(target.end.y));
			out.write(" ");
			target.start.x=Variables.sense_radius+rand.nextInt(Variables.x);
			//System.out.println("target.start.x "+target.start.x);
			target.end.x=Variables.sense_radius+rand.nextInt(Variables.x);
			out.write(Integer.toString(target.start.x));
			out.write(" ");
			out.write(Integer.toString(target.end.x));
			out.write(" ");
			//System.out.println("target.end.x "+target.end.x);
			target.speed=speed;
			target.location.x=target.start.x;
			target.location.y=target.start.y;
			target.getAngle();
			
			targets.add(target);
		}
		else
		{
			out.write("0");
			out.write(" ");
			
		}
		/**
		 * need to add alpha & beta mistakes
		 */
		//end of a line- every line is one time-step
		out.newLine();
		out.flush();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/*public static void main(String[] args) {
		// TODO Auto-generated method stub

	}*/
	
	public void generateTargets()
	{
		int number;
		Target target=null;
		try{
			
			
			String line=in.readLine();
			//System.out.println(line);
			StringTokenizer tokens=new StringTokenizer(line);
			number=Integer.parseInt(tokens.nextToken());
			for(int i=0;i<number;i++)
			{
				target=new Target();
				target.volume=Integer.parseInt(tokens.nextToken());
				target.start_time=Time.time;
				target.start.y=Integer.parseInt(tokens.nextToken());
				target.end.y=Integer.parseInt(tokens.nextToken());
				target.start.x=Integer.parseInt(tokens.nextToken());
				target.end.x=Integer.parseInt(tokens.nextToken());
				target.speed=speed;
				target.location.x=target.start.x;
				target.location.y=target.start.y;
				target.getAngle();
				
				targets.add(target);
			}

			
			
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public void startWorldFrame()
	{
		frame=new JFrame("World");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(950,120);
		frame.setLayout(new BorderLayout());
		WorldPanel panel=new WorldPanel(/*envi,*/this);
		//panel.p
		frame.add(panel,"Center");
		
		//frame.setBounds(30, 90, 700, 450);
		frame.pack();
		frame.setVisible(true);
		
	}
	
	public void timeStep()
	{
		int i=0;
		int j=0;
		Target target;
		//System.out.print(targets.size()+" ");
		for(i=0;i<targets.size();i++)
		{
			j++;
			target=targets.get(i);
			if(!target.setNewLocation())
			{
				targets.remove(i);
				i--;
			}
		}
		//System.out.println(j);
	}
}
